/**
 * Created by apple on 16/9/29.
 */
var target_id = '';
var attention_total = '';//关注的人的总数

window.onload = function () {
    if (user_id) {
        data_motocycle = {
            user_id: user_id,
            page: page,
            rows: 10
        };
        attention_content();

        var _pagenum = Math.ceil(total / data_motocycle.rows);
        if (_pagenum > 1) {
            //需要分页展示
            $('.pageNum').show();
            $(".pageNum").createPage({
                pageCount: _pagenum,
                current: data_motocycle.page,
                backFn: function (p) {
                    //单击回调方法，p是当前页码
                    data_motocycle.page = p;
                    attention_content();
                }
            });
        }
    } else {
        window.location.href = "loading.html"
    }
};


//关注的人(具体文章信息列表)
function attention_content() {
    $.ajax({
        type: 'get',
        url: url_link + 'articles/follow',
        data: data_motocycle,
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var attention_list = "";
                if (obj.code == 0) {
                    total = obj.data.total;
                    if (obj.data.total != 0) {
                        $.each(obj.data.rows, function (i, item) {
                            attention_list += "<li class='list' id=" + i + ">" +
                                "<a href='javascript:;' class='add-favorite'></a>" +
                                "<div class='user-info'>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' class='user-pic' target='_blank'>" +
                                "<img src=" + item.user.avatar + " style='width: 120px;height: 120px;'>" +
                                "</a> " +
                                "<h5 class='user-name'>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank'>" + item.user.nickname + "</a>" +
                                "</h5>" +
                                "<div class='button-area'>" +
                                "<a href='javascript:;' class='white-button un-follow-btn' id='pay_attention_button' data-target-id=" + item.user_id + " data-user-id=" + data_motocycle.user_id + ">" +
                                "<i></i>取消关注</a>" +
                                "<br>" +
                                "<a href='javascript:;' class='white-button private-btn' id='private_letter_button' style='margin-top: 10px'><i></i>发私信</a>" +
                                "</div>" +
                                "</div>" +
                                "<div class='thread-content' data-article=" + item.article_id + ">" +
                                "<h3>" +
                                "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>";
                            if (item.is_nice == 1) {
                                //判断是否为精华帖
                                attention_list += "<i class='essence'></i>";
                            }
                            attention_list += item.title + "</a></h3><div class='content'>";
                            var _content = item.content.split("$$##MotorTravel##$$");
                            var _count_text = 0;
                            for (var _a = 0; _a < _content.length; _a++) {
                                var n = _content[_a].substring(0, 12);
                                if (n == "$$##Text##$$") {
                                    //有文字
                                    _count_text = 1;
                                    attention_list += "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" +
                                        "<p>" + _content[_a].substring(12, _content[_a].length) + "</p>" +
                                        "</a>";
                                    break;
                                }
                            }
                            //纯图,没有文字的情况下,使用备用文字
                            if (_count_text == 0) {
                                attention_list += "<p>快点击文章,看看里面有什么干货吧~~~</p>";
                            }
                            attention_list += "<ul class='thread-pic-list'>";
                            var _count_image = 0;
                            for (var _b = 0; _b < _content.length; _b++) {
                                var m = _content[_b].substring(0, 13);
                                if (m == "$$##Image##$$") {
                                    //有图
                                    _count_image = _count_image + 1;
                                    var _content_image_description = _content[_b].split("$$##ImageDescription##$$");
                                    var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                    attention_list += "<li>" +
                                        "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" +
                                        "<img src='" + _content_image_url[0].substring(13, _content_image_url[0].length) + " ' id=" + _count_image + " class='thread-pic-list-img'>"
                                }
                            }
                            //纯文字,没有图的情况下,使用备用图片
                            if (_count_image == 0) {
                                attention_list += "<li>" +
                                    "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" +
                                    "<img src='../image/base/none_img.png' class='thread-pic-list-img'>"
                            }
                            attention_list += "</a>" +
                                "</li>" +
                                "</ul>" +
                                "</div>" +
                                "<div class='thread-info'>" +
                                "<span class='pubtime'>" + item.createdDate + "</span>" +
                                "<span class='click-count'><i></i>" + item.see_count + "</span>" +
                                "<span class='comment-count'><i></i>" + item.comment_count + "</span>" +
                                "<span class='praise-count'><i></i>" + item.gift_count + "</span>" +
                                "</div>" +
                                "</div>" +
                                "</li>"
                        });
                        $('.attention_follow_list').html(attention_list);
                        $('.thread-pic-list-img').each(function () {
                            var _img_id = $(this).attr('id');
                            if (_img_id > 3) {
                                //确保只显示三张图片
                                $(this).parent('a').parent('li').hide();
                            }
                        });
                    } else {
                        if (attention_total == 1) {
                            //只关注了一个人,而这一个人恰好又没有发表过文章
                            $('.follow-list').html("<h2 class='none_info'>您关注的好友暂时没有文章发布</h2>");
                        } else {
                            $('.follow-list').html("<h2 class='none_info'>您还没有关注任何好友哦</h2>");
                        }
                    }
                }
            } catch (e) {

            }

        }
    })
}




